﻿#region
/**
 * $Date: 0001/1/1
*/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using BuXiaChu.OrderDishes.DAL.DataAccess;
using BuXiaChu.OrderDishes.Model;
#endregion

namespace BuXiaChu.OrderDishes.DAL
{
    public class ApplicationsDAL
    {
        #region private const
        private const string PARAM_APPLICATION_ID = "@ApplicationId";
        private const string PARAM_NAME = "@Name";
        private const string PARAM_WEBSITE = "@Website";
        private const string PARAM_DESCRIPTION = "@Description";

        private static readonly string ConnectionString = SQLHelper.CONNECTION_STRING;
        #endregion

        private static void FillModelFrom(SqlDataReader reader, ApplicationsModel obj)
        {
            #region
            if (reader != null && !reader.IsClosed)
            {
                obj.ApplicationId = reader.IsDBNull(reader.GetOrdinal("ApplicationId")) ? Guid.NewGuid() : reader.GetGuid(reader.GetOrdinal("ApplicationId"));
                obj.Name = reader.IsDBNull(reader.GetOrdinal("Name")) ? String.Empty : reader.GetString(reader.GetOrdinal("Name"));
                obj.Website = reader.IsDBNull(reader.GetOrdinal("Website")) ? String.Empty : reader.GetString(reader.GetOrdinal("Website"));
                obj.Description = reader.IsDBNull(reader.GetOrdinal("Description")) ? String.Empty : reader.GetString(reader.GetOrdinal("Description"));
            }
            #endregion
        }

        private static SqlParameter[] GetApplicationsParams(ApplicationsModel obj)
        {
            #region
            SqlParameter[] dbParams ={					
					 SQLHelper.MakeParam(PARAM_APPLICATION_ID, SqlDbType.UniqueIdentifier,0,obj.ApplicationId==Guid.Empty?Guid.NewGuid():obj.ApplicationId),
					 SQLHelper.MakeParam(PARAM_NAME, SqlDbType.NVarChar, 50,obj.Name),					
					 SQLHelper.MakeParam(PARAM_WEBSITE, SqlDbType.NVarChar, 256,obj.Website),					
					 SQLHelper.MakeParam(PARAM_DESCRIPTION, SqlDbType.NVarChar, 256,obj.Description)					
			};

            return dbParams;
            #endregion
        }

        public static bool Save(ApplicationsModel obj)
        {
            #region
            try
            {
                SqlParameter[] dbParams = GetApplicationsParams(obj);

                if (obj.ApplicationId == Guid.Empty)
                {
                    var result = SQLHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "Applications_Insert", dbParams);
                    if (result != null)
                    {
                        obj.ApplicationId = new Guid(result.ToString());
                    }
                    return obj.ApplicationId != Guid.Empty ? true : false;
                }
                else
                {
                    var affectNum = SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "Applications_Update", dbParams);
                    return affectNum > 0 ? true : false;
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("Applications-->InsertOrUpdate-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }

            return false;
            #endregion
        }

        public static ApplicationsModel GetApplicationsById(Guid applicationId)
        {
            #region
            SqlDataReader reader = null;
            ApplicationsModel obj = new ApplicationsModel();

            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_APPLICATION_ID, SqlDbType.Int, 0, applicationId)
                    };

                reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Applications_GetApplicationsById", dbParams);

                if (reader.Read())
                {
                    FillModelFrom(reader, obj);
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("Applications-->GetSingleObject-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();
            }

            return obj;
            #endregion
        }

        public static bool Delete(Guid applicationId)
        {
            #region
            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_APPLICATION_ID, SqlDbType.Int, 0, applicationId)
                    };

                return SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "Applications_Delete", dbParams) > 0 ? true : false;
            }
            catch (Exception e)
            {
                Exception ex = new Exception("Applications-->Delete-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }
            return false;
            #endregion
        }

        public static List<ApplicationsModel> Search(string sqlWhere, int pageIndex, int pageSize, out int pageCount)
        {
            #region
            List<ApplicationsModel> result = new List<ApplicationsModel>();
            pageCount = 0;
            try
            {
                if (string.IsNullOrEmpty(sqlWhere))
                {
                    sqlWhere = " 1=1 ";
                }

                SqlParameter[] dbParams =
                {
                    SQLHelper.MakeParam("@sqlWhere", SqlDbType.NVarChar,0,sqlWhere),
                    SQLHelper.MakeParam("@pageIndex",SqlDbType.Int,0,pageIndex),
                    SQLHelper.MakeParam("@pageSize",SqlDbType.Int,0,pageSize),
                    SQLHelper.MakeOutParam("@pageCount", SqlDbType.Int,0,0)                    
                };
                using (SqlDataReader reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Applications_Search", dbParams))
                {
                    while (reader.Read())
                    {
                        ApplicationsModel model = new ApplicationsModel();
                        FillModelFrom(reader, model);
                        result.Add(model);
                    }
                }
                pageCount = int.Parse(dbParams[3].Value.ToString());
            }
            catch (Exception e)
            {
                Exception ex = new Exception("Applications-->Search-->" + e.ToString());
                ExceptionMessageDAL.Record(ex);
            }
            return result;
            #endregion
        }
        public static List<ApplicationsModel> GetAll()
        {
            #region
            List<ApplicationsModel> result = new List<ApplicationsModel>();
            try
            {
                using (SqlDataReader reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.Text, "select * from Applications", null))
                {
                    while (reader.Read())
                    {
                        ApplicationsModel model = new ApplicationsModel();
                        FillModelFrom(reader, model);
                        result.Add(model);
                    }
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("Applications-->GetAll-->" + e.ToString());
                ExceptionMessageDAL.Record(ex);
            }
            return result;
            #endregion
        }
    }
}
